/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import book.PhoneBookItem;
import java.io.IOException;
import worker.Collector;

/**
 *
 * @author x3m
 */
public class editBean extends MainBean {

    private String PageName = "JSP Page";
    private String PageTitle = "";
    private String PageSubmitButton = "";

    public String getPageName() {
        return PageName;
    }

    @Override
    public boolean isLoad() {

        session.removeAttribute("surname");
        session.removeAttribute("firstname");
        session.removeAttribute("secondname");
        session.removeAttribute("tel1");
        session.removeAttribute("tel2");
        session.removeAttribute("tel3");
        session.removeAttribute("addr");

        if ("true".equals(getStringFromParam("doEdit")) && !getStringFromParam("currentID").isEmpty()) {
            PhoneBookItem item = new PhoneBookItem();
            item.setPrivateID(getStringFromParam("currentID"));
            item.setSurname(getStringFromParam("surname"));
            item.setFirst_name(getStringFromParam("firstname"));
            item.setSecond_name(getStringFromParam("secondname"));
            item.setPhone1(getStringFromParam("tel1"));
            item.setPhone2(getStringFromParam("tel2"));
            item.setPhone3(getStringFromParam("tel3"));
            item.setAddr(getStringFromParam("addr"));
                        
            Boolean result = Collector.editItem(item);
            PageName = result.toString();
            
            try {
                response.sendRedirect("index.jsp");
                //Collector.generateUsers(1);
            } catch (IOException ex) {
            }            
        }

        if ("true".equals(getStringFromParam("doAdd"))) {
            PhoneBookItem item = new PhoneBookItem();
            item.setSurname(getStringFromParam("surname"));
            item.setFirst_name(getStringFromParam("firstname"));
            item.setSecond_name(getStringFromParam("secondname"));
            item.setPhone1(getStringFromParam("tel1"));
            item.setPhone2(getStringFromParam("tel2"));
            item.setPhone3(getStringFromParam("tel3"));
            item.setAddr(getStringFromParam("addr"));
                        
            Boolean result = Collector.addItem(item);
            PageName = result.toString();
            try {
                response.sendRedirect("index.jsp");
                //Collector.generateUsers(1);
            } catch (IOException ex) {
            }            
        }        
        
        if ("add".equals(getStringFromParam("action"))) {
            PageTitle = "<h1>Добавление пользователя</h1>";
            PageSubmitButton = "<input type='hidden' name='doAdd' value='true' /><input type='submit' value='Добавить' />";
            return true;
        }

        if ("edit".equals(getStringFromParam("action")) && !getStringFromParam("id").isEmpty()) {
            PageTitle = "<h1>Редактирование пользователя</h1>";
            PageSubmitButton = "<input type='hidden' name='doEdit' value='true' /><input type='submit' value='Редактировать' />";
            
            PhoneBookItem item = Collector.getItem(getStringFromParam("id"));
            session.setAttribute("currentID", item.getPrivateID());
            session.setAttribute("surname", item.getSurname());
            session.setAttribute("firstname", item.getFirst_name());
            session.setAttribute("secondname", item.getSecond_name());
            session.setAttribute("tel1", item.getPhone1());
            session.setAttribute("tel2", item.getPhone2());
            session.setAttribute("tel3", item.getPhone3());
            session.setAttribute("addr", item.getAddr());
            
            return true;
        }
        
        return false;
    }

    public String getPageTitle() {
        return PageTitle;
    }

    public String getPageSubmitButton() {
        return PageSubmitButton;
    }

    public String getForm() {
        
        String code = "";
        code += "<input type='hidden' name='currentID' value='"+ ((session.getAttribute("currentID") != null) ? session.getAttribute("currentID") : "") +"' />";
        code += "<input id='surname' name='surname' type='text' maxlength='200' value='" + ((session.getAttribute("surname") != null) ? session.getAttribute("surname") : "") + "' />";
        code += "<label for='surname' >Фамилия</label>";
        code += "<br />";
        code += "<input id='firstname' name='firstname' type='text' maxlength='200' value='" + ((session.getAttribute("firstname") != null) ? session.getAttribute("firstname") : "") + "' />";
        code += "<label for='firstname' >Имя</label>";
        code += "<br />";
        code += "<input id='secondname' name='secondname' type='text' maxlength='200' value='" + ((session.getAttribute("secondname") != null) ? session.getAttribute("secondname") : "") + "' />";
        code += "<label for='secondname' >Отчество</label>";
        code += "<br />";
        code += "<input id='tel1' name='tel1' type='text' maxlength='200' value='" + ((session.getAttribute("tel1") != null) ? session.getAttribute("tel1") : "") + "' />";
        code += "<label for='tel1' >Телефон №1</label>";
        code += "<br />";
        code += "<input id='tel2' name='tel2' type='text' maxlength='200' value='" + ((session.getAttribute("tel2") != null) ? session.getAttribute("tel2") : "") + "' />";
        code += "<label for='tel2' >Телефон №2</label>";
        code += "<br />";
        code += "<input id='tel3' name='tel3' type='text' maxlength='200' value='" + ((session.getAttribute("tel3") != null) ? session.getAttribute("tel3") : "") + "' />";
        code += "<label for='tel3' >Телефон №3</label>";
        code += "<br />";
        code += "<input id='addr' name='addr' type='text' maxlength='200' value='" + ((session.getAttribute("addr") != null) ? session.getAttribute("addr") : "") + "' />";
        code += "<label for='addr' >Адрес</label>";
        
        return code;
    }
}
